﻿using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using SweetSurvey.Models.Repositories;

namespace SweetSurvey
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Published",
                "{id}/Published",
                new { controller = "Survey", action = "Published" }
            );

            routes.MapRoute(
                "EditSurvey",
                "{id}/Edit",
                new { controller = "Survey", action = "Edit" }
            );

            routes.MapRoute(
                "FillOut",
                "{id}/FillOut",
                new { controller = "Survey", action = "FillOut" }
            );

            routes.MapRoute(
                "Submitted",
                "{id}/Submitted",
                new { controller = "Survey", action = "Submitted" }
            );

            routes.MapRoute(
                "Report",
                "{id}/Report",
                new { controller = "Survey", action = "Report" }
            );

            routes.MapRoute(
                "FirstLevelPages",
                "{action}",
                new { controller = "Home" }
            );

            routes.MapRoute(
                "Administrator",
                "Administrator/{action}",
                new { controller = "Administrator" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        public static void InitIoCContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterModule(new DefaultModule());
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            InitIoCContainer();
        }
    }

    public class DefaultModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            var assembly = Assembly.GetExecutingAssembly();
            builder.RegisterControllers(assembly);
            builder.RegisterAssemblyTypes(assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
        }
    }
}